var t = require("../../requests/request.js");

getApp();

Page({
    data: {
        currentTab: 0,
        swiperTab: 0,
        winWidth: 0,
        winHeightAuto: 0,
        winHeight: 0,
        autoplay: !0,
        interval: 5e3,
        duration: 1e3,
        swiperTopCurrent: 0,
        imgUrls: [ "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", "http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg", "http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg" ],
        defaultImg: [ "http://res.app.yiche.com/wechatapp/async90x60@2x.png" ],
        cateid: [ 1e4, 3, 1, 2, 2e3 ],
        videocat: "1,6,7",
        pageindex: 1,
        pagesize: 25,
        FocusData: [],
        newsList: [],
        newcar: [],
        pingce: [],
        daogou: [],
        videocatinfo: [],
        videolist1: [],
        videolist6: [],
        videolist7: [],
        scrolltolowerstatus: 0
    },
    onLoad: function(t) {
        var a = this;
        wx.canIUse("showLoading") && wx.showLoading(), wx.canIUse("showNavigationBarLoading") && wx.showNavigationBarLoading(), 
        this.GetNewsData(0, !1), wx.getSystemInfo({
            success: function(t) {
                /(iPhone|iPad|iPod|iOS)/i.test(t.system) ? a.setData({
                    winHeightAuto: t.windowHeight - 44
                }) : a.setData({
                    winHeightAuto: t.windowHeight
                });
            }
        });
    },
    GetNewsData: function(a, e) {
        console.log("pageindex=" + this.data.pageindex + ";rand=" + a + ";isappend=" + e);
        var o = this, i = o.data.currentTab;
        4 == i ? this.GetVideoData() : t.getNewsList(o.data.cateid[i], o.data.pageindex, o.data.pagesize, a, function(t) {
            o.data.scrolltolowerstatus > 0 && (o.data.scrolltolowerstatus = 0), console.log(t);
            var a = t.Data;
            if (0 == i) {
                var n = t.FocusData;
                e ? o.setData({
                    newsList: o.data.newsList.concat(a)
                }) : o.setData({
                    newsList: a,
                    FocusData: n
                });
            } else 1 == i ? e ? o.setData({
                newcar: o.data.newcar.concat(a)
            }) : o.setData({
                newcar: a
            }) : 2 == i ? e ? o.setData({
                pingce: o.data.pingce.concat(a)
            }) : o.setData({
                pingce: a
            }) : 3 == i && (e ? o.setData({
                daogou: o.data.daogou.concat(a)
            }) : o.setData({
                daogou: a
            }));
        }, function() {
            console.log("complete.");
        }, function() {
            console.log("complete."), wx.canIUse("hideLoading") && wx.hideLoading(), wx.canIUse("hideNavigationBarLoading") && wx.hideNavigationBarLoading();
        });
    },
    GetVideoData: function() {
        var a = this;
        t.getVideocatlist(a.data.videocat, function(t) {
            console.log(t);
            var e = t.Data;
            if (e) {
                for (var o = [], i = [], n = [], s = 0; s < 4; s++) {
                    if (e[0] && e[0].CatList.length > s) {
                        r = e[0].CatList[s].TotalVisit;
                        e[0].CatList[s].TotalVisit = r > 1e4 ? (r / 1e4).toFixed(1) + "万" : r, o.push(e[0].CatList[s]);
                    }
                    if (e[1] && e[1].CatList.length > s) {
                        r = e[1].CatList[s].TotalVisit;
                        e[1].CatList[s].TotalVisit = r > 1e4 ? (r / 1e4).toFixed(1) + "万" : r, i.push(e[1].CatList[s]);
                    }
                    if (e[2] && e[2].CatList.length > s) {
                        var r = e[2].CatList[s].TotalVisit;
                        e[2].CatList[s].TotalVisit = r > 1e4 ? (r / 1e4).toFixed(1) + "万" : r, n.push(e[2].CatList[s]);
                    }
                }
                a.setData({
                    videolist1: o,
                    videolist6: i,
                    videolist7: n,
                    videocatinfo: e
                });
            }
        }, function() {
            console.log("getVideocatlist complete.");
        }, function() {
            console.log("getVideocatlist complete."), wx.canIUse("hideLoading") && wx.hideLoading(), 
            wx.canIUse("hideNavigationBarLoading") && wx.hideNavigationBarLoading();
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        var t = +new Date();
        console.log("onPullDownRefresh t=" + t), this.GetNewsData(t, !1), wx.stopPullDownRefresh();
    },
    onReachBottom: function() {
        console.log("onReachBottom");
    },
    onShareAppMessage: function() {
        return {
            title: "专业的汽车资讯"
        };
    },
    scrolltoupper: function(t) {
        var a = +new Date();
        console.log("scrolltoupper t=" + a), this.GetNewsData(a, !1), wx.showNavigationBarLoading();
    },
    scrolltolower: function(t) {
        var a = this;
        if (console.log("scrolltolower=" + a.data.scrolltolowerstatus), 0 == a.data.scrolltolowerstatus) {
            a.data.scrolltolowerstatus = 1;
            var e = a.data.pageindex + 1;
            a.setData({
                pageindex: e
            }), a.GetNewsData(0, !0);
        }
        wx.showNavigationBarLoading();
    },
    swiperchange: function(t) {
        var a = this;
        0 == t.detail.current ? a.setData({
            autoplay: !0
        }) : a.setData({
            autoplay: !1
        }), a.setData({
            currentTab: t.detail.current,
            pageindex: 1
        }), console.log(a.data), 0 == t.detail.current && a.data.newsList && a.data.newsList.length > 0 || 1 == t.detail.current && a.data.newcar && a.data.newcar.length > 0 || 2 == t.detail.current && a.data.pingce && a.data.pingce.length > 0 || 3 == t.detail.current && a.data.daogou && a.data.daogou.length > 0 || 4 == t.detail.current && a.data.videolist1 && a.data.videolist1.length > 0 || (this.GetNewsData(0, !1), 
        console.log("滑动进:" + t.detail.current), wx.canIUse("showLoading") && wx.showLoading(), 
        wx.canIUse("showNavigationBarLoading") && wx.showNavigationBarLoading());
    },
    swichNav: function(t) {
        console.log(this.data.currentTab);
        var a = this;
        0 == t.detail.current ? a.setData({
            autoplay: !0
        }) : a.setData({
            autoplay: !1
        }), this.data.currentTab === t.currentTarget.dataset.current ? a.setData({
            pageindex: 1
        }) : a.setData({
            currentTab: t.currentTarget.dataset.current,
            swiperTab: t.currentTarget.dataset.current,
            pageindex: 1
        }), console.log("点击进:" + t.currentTarget.dataset.current);
    },
    swiperTopChange: function(t) {
        this.setData({
            swiperTopCurrent: t.detail.current
        });
    },
    toDetailPage: function(t) {
        var a = this, e = t.currentTarget.dataset.id, o = parseInt(t.currentTarget.dataset.index) - 1, i = parseInt(t.currentTarget.dataset.cat), n = 1 == i ? 0 : 6 == i ? 1 : 2;
        console.log("catindex=" + n + "cat=" + i + ";index=" + o);
        var s = a.data.videocatinfo[n].CatList[o], r = "id=" + e + "&Author=" + s.Author + "&PublishTime=" + s.PublishTime;
        r += "&TotalVisit=" + s.TotalVisit + "&CategoryName=" + s.CategoryName + "&Duration=" + s.Duration + "&Title=" + s.Title + "&Mp4Link=" + s.Mp4Link + "&imgelink=" + s.ImageLink, 
        wx.navigateTo({
            url: "../video/video?" + r
        });
    },
    toListPage: function(t) {
        var a = t.currentTarget.dataset.catid, e = t.currentTarget.dataset.catname;
        console.log(t.currentTarget.dataset.catname), console.log(t.currentTarget.dataset.catid), 
        wx.navigateTo({
            url: "../videoList/videoList?catid=" + a + "&catname=" + e
        });
    },
    toNewsDetail: function(t) {
        var a = t.currentTarget.dataset.id, e = t.currentTarget.dataset.type;
        wx.navigateTo({
            url: "../newsDetail/newsDetail?newsid=" + a + "&newstype=" + e
        });
    },
    binderrorimg: function(t) {
        console.log(t.target.dataset.index);
        var a = this, e = parseInt(t.target.dataset.index), o = parseInt(t.target.dataset.img), i = parseInt(a.data.currentTab);
        if (console.log("binderrorimg" + i), 0 == i) if (3 == o) {
            var n = a.data.FocusData;
            n[e].PicCover = a.data.defaultImg[0], a.setData({
                FocusData: n
            });
        } else {
            var s = a.data.newsList;
            2 == s[e].Type ? s[e].ImageListInfo[o].CoverImg = a.data.defaultImg[0] : s[e].PicCover = a.data.defaultImg[0], 
            a.setData({
                newsList: s
            });
        } else if (1 == i) {
            var r = a.data.newcar;
            2 == r[e].Type ? r[e].ImageListInfo[o].CoverImg = a.data.defaultImg[0] : r[e].PicCover = a.data.defaultImg[0], 
            a.setData({
                newcar: r
            });
        } else if (2 == i) {
            var d = a.data.pingce;
            2 == d[e].Type ? d[e].ImageListInfo[o].CoverImg = a.data.defaultImg[0] : d[e].PicCover = a.data.defaultImg[0], 
            a.setData({
                pingce: d
            });
        } else if (3 == i) {
            var c = a.data.daogou;
            2 == c[e].Type ? c[e].ImageListInfo[o].CoverImg = a.data.defaultImg[0] : c[e].PicCover = a.data.defaultImg[0], 
            a.setData({
                daogou: c
            });
        }
    }
});